<?php
header("content-type:text/html;charset=utf-8");
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class postsAction extends publicAction
{
    public function _empty()
    {
        header("HTTP/1.0 404 Not Found");
        $this->display('public:404');
    }
    public function postsDetail(){
        $forum_id=$_GET['forum_id'];
        $user_id=$_GET['author_id'];
        $posts_id=$_GET['posts_id'];
        $forum=$this->getForumBypostId($forum_id);
        $author=$this->getUserById($user_id);
        $list_page=$this->getReplyById($posts_id);
        $this->assign('forum',$forum);
        $this->assign('author',$author);
        $this->assign('reply',$list_page['list']);
        $this->assign('page',$list_page['page']);
        $this->assign('reply_count',$list_page['count']);
        $this->posts=D('posts')->where("id=$posts_id")->find();
        $this->display();exit();
    }
    public function getPostsByField(){
        $whereMap=array();
        if($_GET['posts_id']){
            $whereMap['id']=$_GET['posts_id'];
        }
        if($_GET['theme']){
            $whereMap['theme']=$_GET['theme']; 
        }
        $posts=D('posts')->where($whereMap)->find();
        if($posts){
                   echo "<pre>";print_r($posts);echo "</pre>";exit();
            $this->display('postsDetail');
        }
    }
    public function postsSearch(){
        $whereMap=array('theme'=>array('like',"%{$_POST['theme']}%"));
        $posts=M('posts');
        import('ORG.Util.Page');
        $count=$posts->where($whereMap)->count();
        $page=new Page($count,5);
        $show=$page->show();
        $postsList=$posts->where($whereMap)->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('show',$show);
        $this->assign('posts',$postsList);
        $this->display();
        exit(); 
    }
    public function postsPublish(){
        if($_POST){
            $url=$_POST['url'];
            unset($_POST['url']);
            $data['theme']=$_POST['theme'];
            $data['mood']=$_POST['mood'];
            $data['forum_id']=$_POST['forum_id'];
            $data['posts_id']=$_POST['posts_id'];
            $data['forum_name']=$_POST['forum_name'];
            //$data['content']=!empty($_POST['l_content'])?$_POST['l_content']:$_POST['h_content'];
            $data['content']=$_POST['content'];
            $data['author_id']=$_SESSION['userInfo']['id'];
            $data['author']=$_SESSION['userInfo']['username'];
            $data['dateline']=date("Y-m-d H:i:s",time());
            $res=D('posts')->data($data)->add();
            if($res){
                D('forum')->where("id={$_POST['forum_id']}")->setInc('theme_count',1);//增加对应版块的主题数
                redirect($url);exit();
            }else{
                $this->assign('error','<font color="red">发帖失败</font>');
                $this->display();
            }
        }
    }

     public function submitReply(){
        if($_POST){
            $data=array(
                'author_id'=>$_SESSION['userInfo']['id'],
                'author'=>$_SESSION['userInfo']['username'],
                'theme_id'=>$_POST['theme_id'],
                'content'=>$_POST['content'],
                'mood'=>$_POST['mood'],
                'dateline'=>date('Y-m-d H:i:s',time())
            );
            $res=D('reply')->data($data)->add();
            $param="posts_id={$_POST['theme_id']}&author_id={$_POST['author_id']}&forum_id={$_POST['forum_id']}";
            if($res){
                redirect("/User/posts/postsDetail?$param");
                exit();
            }else{
                $this->assign('error',"<font color='red'>提交回复失败</font>");
                redirect("/User/posts/postsDetail?$param");
                exit();
            }
        }
    }
    public function editReply(){
        $whereMap=array('id'=>$_POST['id']);
        $param="posts_id={$_POST['posts_id']}&author_id={$_POST['author_id']}&forum_id={$_POST['forum_id']}";
        $data=array(
            'dateline'=>date('Y-m-d H:i:s',time()),
            'content'=>$_POST['content']
        );
        D('reply')->where($whereMap)->data($data)->save();
        redirect("postsDetail?$param");
    }
    public function editPosts(){
        $whereMap=array('id'=>$_POST['posts_id']);
        $param="posts_id={$_POST['posts_id']}&author_id={$_POST['author_id']}&forum_id={$_POST['forum_id']}";
        $data=array(
            'dateline'=>date('Y-m-d H:i:s',time()),
            'content'=>$_POST['content']
        );
        D('posts')->where($whereMap)->data($data)->save();
        redirect("postsDetail?$param");
    }
    public function goodPosts(){
        if($_GET){
            $whereMap=array('id'=>$_GET['id']);
            $this->incGood($_GET['forum_id']);
            $this->res=D('posts')->where($whereMap)->setField('is_good','yes');
            if($this->res){
                redirect($_GET['url']);
                exit();
            }
        }
    }
    public function cancelGood(){
        if($_GET){
            $whereMap=array('id'=>$_GET['id']);
            $this->res=D('posts')->where($whereMap)->setField('is_good','no');
            $this->decGood($_GET['forum_id']);
            if($this->res){
                redirect($_GET['url']);
                exit();
            }
        }
    }
    public function postsDrop(){
        if($_GET['id']){
            $whereMap=array(
                'id'=>$_GET['id']
            );
            $this->res=D('posts')->where($whereMap)->delete();
            if($this->res){
                $this->assign('success',' 删除成功');
                redirect('postsList');
            }else{
                $this->assign('error',' 删除失败');
                redirect('postsList');
            }
        }
    }
    
}
?>
